package com.cns.android.onlineservice;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.online.ICNSOnLineService;

/**
 * The {@code OnlineServiceManager} class is used to manage onlineservice.
 * To get an instance of this class, use {@link #getInstance(Context)}.
 */
public class OnlineServiceManager {
    private static final String LOG_TAG = "OnlineServiceManager";

    private static ICNSOnLineService mCNSOnLineService = null;

    private Context mContext;

    private static OnlineServiceManager instance = null;

    private OnlineServiceManager(Context context) {
        mContext = context;
    }

    /**
     * Get an instance of {@code OnlineServiceManager}.
     *
     * @param context The {@link Context} to use when necessary.
     * @return An instance of {@code OnlineServiceManager}.
     */
    public static OnlineServiceManager getInstance(Context context) {
        synchronized (OnlineServiceManager.class) {
            if (instance == null) {
                instance = new OnlineServiceManager(context);
            }

            return instance;
        }
    }

    private ICNSOnLineService getCNSOnLineService(){
        if(mCNSOnLineService==null){
            IBinder binder = ServiceManager.getService(ICNSOnLineService.SERVICE_NAME);
            mCNSOnLineService = ICNSOnLineService.Stub.asInterface(binder);
        }
        return mCNSOnLineService;
    }

    /**
     * Get the current onlineService status
     *
     * @return The onlineService status true is online false is offline
     * @throws RemoteException If a binder remote invocation error occurred.
     */
    public boolean isOnlineServiceOn()throws RemoteException{
        Log.d(LOG_TAG,"isOnlineServiceOn");
        return getCNSOnLineService().isOnlineServiceOn();
    }

    /**
     * get the current variant
     * @return the variant information
     * @throws RemoteException
     * http://cnninvmcnflnc01:8090/display/PCNS3/Concept%3A+Variants
     * variant = 0 for unknown
     * 301001: CHN VW (variant = 1)
     * 301002: HKG/MAC VW (variant = 2)
     * 301003: TWN VW (variant = 3)
     * 301004: TWN Skoda (variant = 4)
     * 301005: CHN Skoda (variant = 5)
     * 701001: CHN VW (variant = 1)
     * 701002: HKG/MAC VW (variant = 2)
     * 701003: TWN VW (variant = 3)
     * 701004: TWN Skoda (variant = 4)
     */
    public int getVariant()throws RemoteException{
        Log.d(LOG_TAG,"isOnlineServiceOn");
        return getCNSOnLineService().getVariant();
    }

    /**
     * get current Manufacturer
     * 0: unknow  1:SVW-VW  2: FAW-VW  3. FBU  3: OTHERS
     *
     * @return the current Manufacturer type
     */
    public int getManufacturer() throws RemoteException{
        Log.d(LOG_TAG,"getManufacturer");
        return getCNSOnLineService().getManufacturer();
    }
}
